package org.dllwh.nacos.sdk;


import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.utils.StringUtils;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName NacosConfigureTemplate
 * @Description Nacos 配置管理 SDK
 * @Date 2025-08-25 14:42
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class NacosConfigureTemplate {
    private final Props props = PropsUtil.get("nacos.properties");

    /**
     * 创建Nacos配置
     */
    public boolean createNacosConfig(String dataId, String group, String content, String type) throws NacosException {
        ConfigService configService = ConfigFactory.createConfigService(props);
        if (StringUtils.isBlank(type)) {
            return configService.publishConfig(dataId, group, content);
        } else {
            return configService.publishConfig(dataId, group, content, type);
        }
    }

    /**
     * 删除Nacos配置
     */
    public boolean deleteNacosConfig(String dataId, String group) throws NacosException {
        ConfigService configService = ConfigFactory.createConfigService(props);
        return configService.removeConfig(dataId, group);
    }

    /**
     * 获取Nacos配置
     */
    public String getNacosConfig(String dataId, String group, long timeoutMs) throws NacosException {
        ConfigService configService = ConfigFactory.createConfigService(props);
        return configService.getConfig(dataId, group, timeoutMs);
    }
}
